package qaz.network;

import qaz.activity.CategoryActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class LoadData extends AsyncTask<String, Void, String> {

	public final static String _INIT = "INIT";
	public final static String _DATA_EXCHANGE="DATA EXCHANGE";
	public static String socketAddress;
	private Context mContext;
	 public LoadData(Context context){
		 this.mContext=context;
	 }
	@Override
	protected String doInBackground(String... arg) {
		// TODO Auto-generated method stub
		Log.i("newfeed",""+arg[0]);
		String rtString=null;
		if (arg[0].compareTo(LoadData._INIT)==0) {
			
			try {

				Connection.init();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (arg[0].compareTo(LoadData._DATA_EXCHANGE)==0) {			
			try {
				Connection.sendData(arg[1]);
				String st=Connection.recvData();
				String utf8 = new String(st.getBytes(), "UTF8");
				rtString=utf8;
				Log.i("newfeed","return data :"+utf8);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rtString;
	}

	protected void onPostExecute(String result) {
		// execution of result of Long time consuming operation
		Log.i("newfeed","runhere");
		Intent i = new Intent(mContext,CategoryActivity.class);
		i.putExtra("INDEX","qazqaz" );
		i.putExtra("CONTENT", result);
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		mContext.startActivity(i);
	}

	protected void onPreExecute() {
		// Things to be done before execution of long running operation. For
		// example showing ProgessDialog
	}

	protected void onProgressUpdate(Void... values) {
		// Things to be done while execution of long running operation is in
		// progress. For example updating ProgessDialog
	}

}
